package com.jiuzhe.commercial.ui.house.editor;

import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import com.jiangyy.easydialog.SingleChoiceDialog;
import com.jiuzhe.commercial.R;
import com.jiuzhe.commercial.adapter.HouseBondListAdapter;
import com.jiuzhe.commercial.base.BaseActivity;
import com.jiuzhe.commercial.bean.CalendarBean;
import com.jiuzhe.commercial.bean.HouseStayListBean;
import com.jiuzhe.commercial.constant.Constants;
import com.jiuzhe.commercial.http.CallBack;
import com.jiuzhe.commercial.http.HttpClient;
import com.jiuzhe.commercial.ui.house.CalendarActivity;
import com.jiuzhe.commercial.utils.RxBus;
import com.jiuzhe.commercial.utils.SPUtil;
import com.jiuzhe.commercial.utils.UIUtils;

import java.util.Calendar;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import rx.Subscription;
import rx.functions.Action1;

/**
 * Created by huanghao on 2018/6/8.
 */

public class EditorHomeBondActivity extends BaseActivity implements View.OnClickListener {
    @BindView(R.id.listview)
    RecyclerView recyclerView;
    @BindView(R.id.iv_not)
    ImageView ivNot;
    private HouseBondListAdapter mHouseAdapter;
    private List<HouseStayListBean.DatePriceListBean> datePriceList;
    private Subscription mSubscription;
    private HouseStayListBean houseStayListBean;

    @Override
    protected void loadViewLayout() {
        setContentView(R.layout.activity_editor_home_bond);
        setTitle("放盘价格");
        setRringTitle("添加");
        tv_right.setOnClickListener(this);
    }

    @Override
    protected void loadData() {
        houseStayListBean = new HouseStayListBean();
        if (null != Constants.HOUSESTAYLISTBEAN)
        datePriceList = Constants.HOUSESTAYLISTBEAN.getDatePriceList();
        recyclerView.setLayoutManager(new LinearLayoutManager(EditorHomeBondActivity.this));
        //设置Item增加、移除动画
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        if (datePriceList !=null && datePriceList.size()>0){
            mHouseAdapter = new HouseBondListAdapter(datePriceList);
            mHouseAdapter.setOnItemClickListener(new HouseBondListAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(View view, int position) {
                    new SingleChoiceDialog.Builder(EditorHomeBondActivity.this).setTitle("提示")
                            .addList(new String[]{"删除"})
                            .setOnItemClickListener(new SingleChoiceDialog.OnItemClickListener() {
                                @Override
                                public void OnItemClick(String title, int position) {
                                    if ("修改".equals(title)){
                                      //  UIUtils.startActivity(CalendarActivity.class);
                                    }else if ("删除".equals(title)){
                                        deleteRoomBond(position);
                                    }
                                }
                            }).show();
                }
            });
        }else {
            ivNot.setVisibility(View.VISIBLE);
        }

        recyclerView.setAdapter(mHouseAdapter);


        mSubscription = RxBus.getInstance().toObserverable(CalendarBean.class)
                .subscribe(new Action1<CalendarBean>() {
                    @Override
                    public void call(CalendarBean bean) {
                        String  price = bean.getPrice();
                        String  startDate = bean.getStartDate();
                        String    endDate = bean.getEndDate();
                        upRoomBond(price,startDate,endDate);
                    }
                });
    }

    /**
     * 修改
     * @param price
     * @param startDate
     * @param endDate
     */
    private void upRoomBond(String price, String startDate, String endDate) {
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        String str1 = startDate.replace("月","-");
        String mStartDate2 = String.valueOf(year) + "-" + str1.replace("日","");
        String str2 = endDate.replace("月","-");
        String mEndDate2 = String.valueOf(year) + "-"+ str2.replace("日","");
        HashMap<String, String> map = new HashMap<>();

        String token = SPUtil.getString(EditorHomeBondActivity.this, Constants.TOKEN);
        String id = SPUtil.getString(EditorHomeBondActivity.this, Constants.ID);
        String autoSignStr = Constants.UPSKUPRICE.substring(Constants.UPSKUPRICE.lastIndexOf("0/") + 1) + "?token=";
        String signStr = UIUtils.Md5(autoSignStr + token);
        map.put("token", id);
        map.put("sign", signStr);
        map.put("skuId", Constants.HOUSESTAYLISTBEAN.getId());
        map.put("beginDate", mStartDate2);
        map.put("endDate", mEndDate2);
        map.put("price",price);

        HttpClient.post(this, Constants.UPSKUPRICE, map, new CallBack<String>() {
            @Override
            public void onSuccess(String result) {
                UIUtils.showToastSafe("添加成功");
                RxBus.getInstance().post(houseStayListBean);
                finish();
            }
        });
    }

    private void deleteRoomBond(final int position) {
        HashMap<String, String> map = new HashMap<>();
        String token = SPUtil.getString(EditorHomeBondActivity.this, Constants.TOKEN);
        String id = SPUtil.getString(EditorHomeBondActivity.this, Constants.ID);
        String autoSignStr = Constants.DELETESKUPRICE.substring(Constants.DELETESKUPRICE.lastIndexOf("0/") + 1) + "?token=";
        String signStr = UIUtils.Md5(autoSignStr + token);
        map.put("token", id);
        map.put("sign", signStr);
        map.put("skuId", Constants.HOUSESTAYLISTBEAN.getId());
        map.put("beginDate", datePriceList.get(position).getStartDate());
        map.put("endDate", datePriceList.get(position).getEndDate());

        HttpClient.post(this, Constants.DELETESKUPRICE, map, new CallBack<String>() {
            @Override
            public void onSuccess(String result) {
                UIUtils.showToastSafe("删除成功");
                RxBus.getInstance().post(houseStayListBean);
                finish();
            }
        });
    }

    @Override
    public void onClick(View v) {
        UIUtils.startActivity(CalendarActivity.class);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mSubscription != null ) {
            if(!mSubscription.isUnsubscribed()){
                mSubscription.unsubscribe();
            }
        }
    }
}
